json=function(str){
        return (new Function('return '+str))();
   };
str=function(json){
    return JSON.stringify(json);
};
function doFilter(loop){
    var exec=function(script){
        eval(script);
    };
	chrome.storage.local.get("adFilterConfig",function(result){
		try{
            if(result&&result.adFilterConfig){
                var config=json(result.adFilterConfig);
                console.log(config);
                if(config.filters){
                    for(let i=0;i<config.filters.length;i++){
                        let filter=config.filters[i];
                        let match=filter.match;
                        let regx=filter.regx;
                        let re = regx&&(regx==true)?new RegExp(match):new RegExp(match.replace('*',".*"));
                        let script=filter.script;
                        let schedule=filter.schedule?filter.schedule:0;
                        let url=window.location.href;
                        if(re.test(url)){
                            if(loop){
                                if(schedule>0){
                                    exec(script);
                                    setInterval(function(){exec(script)},schedule);
                                }
                            }else{
                                exec(script);
                            }
                        }
                    }
                }
            }
		}catch(error){
		    console.error(error);
		}
		if($('html').attr('data-filter-hide')){
		    $('html').attr('data-filter-hide',false);
		    $('html').show();
		}
	});
};
doFilter(false);
window.onload=function(){
    doFilter(true);
};